package avicit.bdp.dgs.qa.service.algo.raw.data.pcm.utils;

import avicit.bdp.dgs.qa.service.algo.raw.data.pcm.entity.PcmParamInfo;

/**
 * @金航数码科技有限责任公司
 * @作者：developer
 * @邮箱：developer@avic-digital.com
 * @创建时间： 2023-11-01
 * @类说明：ComputeUtils
 * @修改记录：
 * @注意事项：
 * @主要功能：PCM数据计算工具类
 */
public class ComputeUtils {
    /**
     * 多项式计算
     *
     * <p>计算公式:y=mx^5 + ax^4+bx^3+cx^2+dx+e m 五次项值 a四次项值 b 三次项值 x 取出的值 c 二次项值 d 一次项值 e 常数项
     * coefficient[6] 依次按照索引定义为：常数项、一次项、二次项、三次项、四次项、五次项
     *
     * @param valueL
     * @return
     */
    public static double poly_number(double[] coefficient, long valueL) {
        double out = coefficient[5] * Math.pow(valueL, 5)
                + coefficient[4] * Math.pow(valueL, 4)
                + coefficient[3] * Math.pow(valueL, 3)
                + coefficient[2] * Math.pow(valueL, 2)
                + coefficient[1] * Math.pow(valueL, 1)
                + coefficient[0];

        return DataConvertUtils.formatByBigDecimal(out, DataConvertUtils.DOUBLELENGTH);
    }

    public static double poly_number(double[] coefficient, int value) {
        double out = coefficient[5] * Math.pow(value, 5)
                + coefficient[4] * Math.pow(value, 4)
                + coefficient[3] * Math.pow(value, 3)
                + coefficient[2] * Math.pow(value, 2)
                + coefficient[1] * Math.pow(value, 1)
                + coefficient[0];

        return DataConvertUtils.formatByBigDecimal(out, DataConvertUtils.DOUBLELENGTH);
    }

    /**
     * d类型参数不需要计算 取出的值就是对应参数值
     *
     * @param value
     * @return
     */
    public static double d_number(int value) {
        // 1、从全帧数据中调用工具类来进行获取参数值 x
        double out = value;

        return DataConvertUtils.formatByBigDecimal(out, DataConvertUtils.DOUBLELENGTH);
    }

    /**
     * d类型参数不需要计算 取出的值就是对应参数值
     *
     * @param valueL
     * @return
     */
    public static double d_number(long valueL) {
        // 1、从全帧数据中调用工具类来进行获取参数值 x
        double out = Double.valueOf(valueL);

        return DataConvertUtils.formatByBigDecimal(out, DataConvertUtils.DOUBLELENGTH);
    }

    public static double[][] getSegemCoefficient(PcmParamInfo paramInfo) {
        double[] codes = paramInfo.codes;
        double[] phys = paramInfo.phys;
        double[][] coefficient = new double[codes.length - 1][codes.length - 1];
        for (int i = 0; i < (codes.length - 1); i++) {
            coefficient[i][0] = (phys[i + 1] - phys[i]) / (codes[i + 1] - codes[i]); // 斜率
            coefficient[i][1] = phys[i] - ((phys[i + 1] - phys[i]) / (codes[i + 1] - codes[i])) * codes[i]; // 截距
        }

        return coefficient;
    }

    public static double getSegemValue(int value, double[][] coefficients) {
        double v = value;
        if (v < coefficients[0][0]) {
            return coefficients[0][0] * v + coefficients[0][1];
        }
        for (int i = 1; i < coefficients.length; i++) {
            if (v < coefficients[i][0] && v > coefficients[i - 1][0]) {
                return DataConvertUtils.formatByBigDecimal(coefficients[i][0] * v + coefficients[i][1], DataConvertUtils.DOUBLELENGTH);
            }
        }

        return DataConvertUtils.formatByBigDecimal(
                coefficients[coefficients.length - 1][0] * v + coefficients[coefficients.length - 1][1], DataConvertUtils.DOUBLELENGTH);
    }

    public static double getSegemValue(long valueL, double[][] coefficients) {
        double v = Double.valueOf(valueL);
        if (v < coefficients[0][0]) {
            return coefficients[0][0] * v + coefficients[0][1];
        }
        for (int i = 0; i < coefficients.length; i++) {
            if (v < coefficients[i + 1][0] && v > coefficients[i][0]) {
                return DataConvertUtils.formatByBigDecimal(coefficients[i][0] * v + coefficients[i][1], DataConvertUtils.DOUBLELENGTH);
            }
        }

        return DataConvertUtils.formatByBigDecimal(
                coefficients[coefficients.length - 1][0] * v + coefficients[coefficients.length - 1][1],
                DataConvertUtils.DOUBLELENGTH);
    }
}
